package com.css.upms.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.common.core.utils.R;
import com.css.common.core.utils.UUIDUtils;
import com.css.common.security.utils.SecurityUtil;
import com.css.upms.api.entity.SysRole;
import com.css.upms.api.entity.SysRoleDept;
import com.css.upms.api.entity.SysRoleMenu;
import com.css.upms.api.entity.SysUserRole;
import com.css.upms.biz.mapper.SysRoleDeptMapper;
import com.css.upms.biz.mapper.SysRoleMapper;
import com.css.upms.biz.mapper.SysRoleMenuMapper;
import com.css.upms.biz.mapper.SysUserRoleMapper;
import com.css.upms.biz.service.SysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色信息表
 *
 * @author hanyx
 * @date 2019-05-03 01:07:32
 */
@Service("sysRoleService")
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    private final SysRoleDeptMapper sysRoleDeptMapper;

    private final SysUserRoleMapper sysUserRoleMapper;


    /**
     * 角色信息表简单分页查询
     *
     * @param sysRole 角色信息表
     * @return
     */
    @Override
    public IPage<SysRole> getSysRolePage(Page<SysRole> page, SysRole sysRole) {
        return baseMapper.getSysRolePage(page, sysRole);
    }

    /**
     * 通过用户ID，查询角色信息
     *
     * @param userId
     * @return
     */
    @Override
    public List listRolesByUserId(String userId) {
        return baseMapper.listRolesByUserId(userId);
    }

    /**
     * 通过角色ID，删除角色,并清空角色菜单缓存
     *
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "menu_details", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public R removeRoleById(String id) {

        int userCount = sysUserRoleMapper.selectCount(Wrappers
                .<SysUserRole>query().lambda().eq(SysUserRole::getRoleId, id));
        if (userCount > 0) {
            return R.buildError("此角色下存在用户,请先转移用户.");
        }

        sysRoleMenuMapper.delete(Wrappers
                .<SysRoleMenu>update().lambda()
                .eq(SysRoleMenu::getRoleId, id));
        sysRoleDeptMapper.delete(Wrappers
                .<SysRoleDept>update().lambda()
                .eq(SysRoleDept::getRoleId, id));
        return R.buildSuccess(this.removeById(id));
    }

    /**
     * 保存角色信息
     *
     * @param role
     * @return
     */
    @Override
    public R saveRole(SysRole role) {
        String roleCode = role.getRoleCode();
        SysRole sysRole = baseMapper.selectOne(Wrappers.<SysRole>query().lambda()
                .eq(SysRole::getRoleCode, roleCode));
        if (sysRole != null) {
            return R.buildError("角色编码已存在");
        }
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(SecurityUtil.getUser().getId());
        role.setRoleId(UUIDUtils.get32UUID());
        this.save(role);
        return R.buildSuccess(role);
    }

}
